package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.OptionsMapper;
import com.tanhua.dubbo.mappers.QuestionnaireMapper;
import com.tanhua.dubbo.mappers.QuestionsMapper;
import com.tanhua.model.db.Options;
import com.tanhua.model.db.Questionnaire;
import com.tanhua.model.db.Questions;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class WordApiImpl implements WordApi {
    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private OptionsMapper optionsMapper;
    @Autowired
    private QuestionsMapper questionsMapper;

    //删除试卷
    @Override
    public void del(Long id) {
        questionnaireMapper.deleteById(id);
        LambdaQueryWrapper<Questions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Questions::getQuestionnaireId, id);
        List<Questions> questions = questionsMapper.selectList(queryWrapper);
        questionsMapper.delete(queryWrapper);


        List<Long> list = CollUtil.getFieldValues(questions, "id", Long.class);
        LambdaQueryWrapper<Options> wrapper = new LambdaQueryWrapper<Options>();
        wrapper.in(Options::getQuestionId, list);
        optionsMapper.delete(wrapper);
    }

    //查询所有问卷
    @Override
    public IPage<Questionnaire> findAll(Integer page, Integer pagesize) {
        Page<Questionnaire> questionnairePage = new Page<>(page, pagesize);

        return questionnaireMapper.selectPage(questionnairePage, null);
    }
}
